package cn.walls1717.wallsmall.order.web;

import cn.walls1717.wallsmall.order.service.OrderService;
import cn.walls1717.wallsmall.order.pojo.vo.OrderConfirmVO;
import cn.walls1717.wallsmall.order.pojo.vo.OrderSubmitRespVO;
import cn.walls1717.wallsmall.order.pojo.vo.OrderSubmitVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;

/**
 * 处理订单
 *
 * @author walls1717
 * @since 2022/11/18 11:13
 **/
@Controller
public class OrderWebController {
    @Resource
    private OrderService orderService;

    /** 去结算 */
    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVO orderConfirmVO = orderService.getOrderConfirm();
        model.addAttribute("orderConfirmData", orderConfirmVO);
        return "confirm";
    }

    /** 下单 */
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVO orderSubmitVO, Model model, RedirectAttributes attributes) {
        OrderSubmitRespVO resp = orderService.submitOrder(orderSubmitVO);
        if (resp.getCode() == 0) {
            model.addAttribute("orderSubmitResp", resp);
            return "pay";
        }
        switch (resp.getCode()) {
            case 1:
                attributes.addFlashAttribute("msg", "令牌对比失败");
                break;
            case 2:
                attributes.addFlashAttribute("msg", "验价失败");
                break;
            case 3:
                attributes.addFlashAttribute("msg", "锁库存失败");
                break;
        }
        return "redirect:http://order.wallsmall.com/toTrade";
    }
}
